package com.nantai.jiafang.nantaijiafang.nt5s.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentUperrorItemBinding;
import com.nantai.jiafang.nantaijiafang.nt5s.bean.Uperror_Item;

import java.util.List;

/**
 * Created by Administrator on 2017/10/27.
 */

public class UperrorsearchAdapter extends RecyclerView.Adapter<UperrorsearchAdapter.AdapterHolder> {
    private Context mContext;
    private List<Uperror_Item> mDataList;
    private FragmentManager mfragmentManager;
    private GridViewAdapter gridViewadapter;
   // private List<ArrayList<String>> listurl;
    public UperrorsearchAdapter(Context context, FragmentManager fragmentManager, List<Uperror_Item> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mfragmentManager=fragmentManager;
      //  this.listurl=urls;
    }
    public UperrorsearchAdapter(Context context, List<Uperror_Item> list) {
        this.mContext = context;
        this.mDataList = list;
      //  this.listurl=urls;
    }

    @Override
    public AdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        FragmentUperrorItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fragment_uperror_item,parent,false);
        return new AdapterHolder(binding);
    }
    @Override
    public void onBindViewHolder(AdapterHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.getmBinding().setNumber(String.valueOf(position+1)+")");
        if (mDataList.get(position).getISOK().equals("1")||mDataList.get(position).getISOK().equals("已处理")) {
            mDataList.get(position).setISOK("已处理");
            holder.getmBinding().isdoing.setTextColor(Color.BLUE);
        }else {
            mDataList.get(position).setISOK("未处理");
            holder.getmBinding().isdoing.setTextColor(Color.RED);
        }
  /*      gridViewadapter=new GridViewAdapter(mContext,listurl.get(position));
        holder.getmBinding().tableLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Data.Selectfragment==0) return;
                if (mDataList.get(position).getISOK().equals("未处理")) {
                    Data.MODE = position;
                    Data.uperror_item = mDataList.get(position);
                    mfragmentManager.beginTransaction()
                            .addToBackStack(null)                    //添加返回键返回
                            .setCustomAnimations(R.anim.in_from_left, R.anim.in_from_right, R.anim.out_from_left, R.anim.out_from_right)
                            .add(R.id.uperror_search, new UpErrorfragment_Add(), "fragment1")
                            .commit();
                }
            }
        });
        holder.getmBinding().tableLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (Data.Selectfragment==0) return true;
                if (mDataList.get(position).getISOK().equals("未处理")) {
                    Data.MODE=position;
                    Data.uperror_item=mDataList.get(position);
                    if (Data.MODE!=-1){
                        mfragmentManager.beginTransaction()
                                .addToBackStack(null)					//添加返回键返回
                                .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                                .add(R.id.uperror_search,new UpErrorfragment_Solve(),"fragment_solve")
                                .commit();
                    }
                }
                return true;
            }
        });
        holder.getmBinding().pictures.setAdapter(gridViewadapter);*/

        holder.getmBinding().setUpitem(mDataList.get(position));
        holder.getmBinding().executePendingBindings();

    }
    public void setprosec(String prodesc,int pos){
        mDataList.get(pos).setProDesc(prodesc);
        notifyDataSetChanged();
    }
    public void setsolve(String analysis,String cause,String solve,String isok,int pos) {
        mDataList.get(pos).setISOK(isok);
        mDataList.get(pos).setAnalysis(analysis);
        mDataList.get(pos).setCause(cause);
        mDataList.get(pos).setSolve(solve);
        notifyDataSetChanged();

    }
    @Override
    public int getItemCount() {
        return mDataList == null ? 0 :mDataList.size();
    }
    static class AdapterHolder extends RecyclerView.ViewHolder {
        // 创建一个静态获取方法
      /*  static AdapterHolder create(LayoutInflater inflater, ViewGroup parent) {
            FragmentUperrorItemBinding binding = FragmentUperrorItemBinding.inflate(inflater, parent, false);
            return new AdapterHolder(binding);
        }*/
        private FragmentUperrorItemBinding mBinding;

        private AdapterHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.mBinding = (FragmentUperrorItemBinding)binding;
        }
        public FragmentUperrorItemBinding getmBinding(){
            return mBinding;
        }
    }
}
